﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Xc.DataAccess.OneNetCore.Services
{
    /// <summary>
    /// 创建设备请求
    /// </summary>
    public class CreateDeviceRequest: RequestCore<CreateDeviceRespond>
    {
        /// <summary>
        /// 设备名称
        /// </summary>
       [JsonPropertyName("title")]
        public string Title { get; set; }
        /// <summary>
        /// 设备描述
        /// </summary>
        [JsonPropertyName("desc")]
        public string? Description { get; set; }
        /// <summary>
        /// 设备标签，字符串数组
        /// </summary>
        [JsonPropertyName("tags")] 
        public string[]? Tags { get; set; }
        /// <summary>
        /// 接入协议
        /// </summary>
        [JsonPropertyName("protocol")] 
        public string Protocol { get; internal set; } = "LWM2M";
        /// <summary>
        /// 坐标信息
        /// </summary>
        [JsonPropertyName("location")] 
        public Location? Location { get; set; }
        /// <summary>
        /// 是否私有字段
        /// </summary>
        [JsonPropertyName("private")] 
        public bool? IsPrivate { get; set; }
        /// <summary>
        /// 认证信息
        /// </summary>
        [JsonPropertyName("auth_info")]
        [JsonConverter(typeof(AuthInfoConverter))]
        public AuthInfo AuthInfo { get; set; }
        /// <summary>
        /// Auth_Code，由数字或字母组成，不超过16位
        /// </summary>
        [JsonPropertyName("auth")]
#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public string AuthCode { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        /// <summary>
        /// 是否订阅设备资源（默认为true）
        /// </summary>
        [JsonPropertyName("obsv")]
        public bool? Observable { get; set; } = true;
        /// <summary>
        /// 芯片类型
        /// </summary>
        [JsonPropertyName("chip")]
        public ChipType? ChipType { get; set; }
        /// <summary>
        /// 其他信息
        /// </summary>
        [JsonPropertyName("other")]
        public Dictionary<string, string>? Other { get; set; } = new Dictionary<string, string>();
        /// <summary>
        /// DTLS加密所使用的PSK，长度8-16个字节，字母或数字组成
        /// </summary>
        [JsonPropertyName("psk")]
        public string? Psk { get; set; }

        protected internal override string RequestPath => "devices";
    }
    public class AuthInfo
    {
        /// <summary>
        /// IMEI
        /// </summary>
        public string IMEI { get; set; }
        /// <summary>
        /// IMSI
        /// </summary>
        public string IMSI { get; set; }
    }
    public class AuthInfoConverter : JsonConverter<AuthInfo>
    {
        public override AuthInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            AuthInfo authInfo = new AuthInfo();
            while (reader.Read())
            {
                if (reader.TokenType==JsonTokenType.EndObject)
                {
                    return authInfo;
                }
                if (reader.TokenType==JsonTokenType.StartObject)
                {
                    continue;
                }
                if (reader.TokenType==JsonTokenType.PropertyName)
                {
                    authInfo.IMEI = reader.GetString();
                }
                if (reader.TokenType==JsonTokenType.String)
                {
                    authInfo.IMSI = reader.GetString();
                }
            }
            return authInfo;
        }

        public override void Write(Utf8JsonWriter writer, AuthInfo value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            writer.WriteString(value.IMEI, value.IMSI);
            writer.WriteEndObject();
        }
    }
}
